package com.sun.talk.utils

import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.stereotype.Component

@Component
object SpringUtils : ApplicationContextAware {
    var mApplicationContext: ApplicationContext? = null

    // 通过name获取 Bean.
    fun getBean(name: String): Any {
        return getApplicationContext()!!.getBean(name)
    }

    // 通过class获取Bean.
    fun <T> getBean(clazz: Class<T>): T {
        return getApplicationContext()!!.getBean(clazz)
    }

    // 通过name,以及Clazz返回指定的Bean
    fun <T> getBean(name: String, clazz: Class<T>): T {
        return getApplicationContext()!!.getBean(name, clazz)
    }

    // 获取applicationContext
    private fun getApplicationContext(): ApplicationContext? {
        return mApplicationContext
    }

	@Throws(BeansException::class)
	override fun setApplicationContext(applicationContext: ApplicationContext) {
		mApplicationContext = applicationContext
	}
}